<head><title>Please wait ...</title>
<script type="text/javascript">
<!--
function fail(message){
	alert(message);
	self.location.href="/php/main.php";
}
//-->
</script>
<?php
$con=mysql_connect("localhost","William");

if (!$con){
	die("MySQL Error " . mysql_error());
}
mysql_select_db("osa", $con);
$success=false;
if (/*(($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)*/true)
{
	if (count($_FILES)==0 || $_FILES["file"]["error"]>0){ ?>
</head><body onLoad='fail("File upload failed!")' />
	<?php die(); }
	else
	{
		$UID = $_REQUEST["UID"];
		$points = $_REQUEST["points"];
		$keywords = $_REQUEST["keywords"];
		$origname = $_FILES["file"]["name"];
		/*$type = ";";
		for($i=0;$i<count($_REQUEST["type"]);$i++)
		if(isset($_REQUEST["type"][$i])) $type = $type . $_REQUEST["type"][$i] . ";";*/
		$type = join(";", $_REQUEST["type"]);
		$query = mysql_query("SELECT * FROM resource WHERE UID=$UID AND name='$origname'");
		if(!$query) die("MySQL Error " . mysql_error());
		$row=mysql_fetch_array($query);
		if($row){ ?>
</head><body onLoad='fail("File <?php echo $origname; ?> already exists!")' />
		<?php die(); } 
		$filename = time() . "~" . $_REQUEST["UID"] . "~" . $_FILES["file"]["name"];
		/*echo "Upload: " . $_FILES["file"]["name"] . "<br />";
		echo "Type: " . $_FILES["file"]["type"] . "<br />";
		echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
		echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";*/

		if (file_exists("../upload/" . $filename))
		{
			die("Internal error: $filename already exists.");
		}
		else
		{
			$query = mysql_query("INSERT INTO resource VALUES (NULL, '$origname', $UID, $points, '$type', '$filename', '$keywords', NOW(), FALSE)");
			if(!$query) die("MySQL Error " . mysql_error());
			move_uploaded_file($_FILES["file"]["tmp_name"], "../upload/" . $filename);
			chmod("../upload/" . $filename, 0664);
			$success=true;
		/*echo "Stored in: " . "upload/" . $filename;*/
		}
	}
}
else
{
die();
}
if($success){ ?>
<meta http-equiv='Refresh' content='5;url=/php/main.php'></head>
<body>File upload success.<br />
You will be redirected to the main page in 5 seconds ...
Click <a href='/php/main.php'>here</a> to go to the login page immediately.</body>
<?php } ?>
